package com.example.orderflow.controller.ordergenerate;

import com.example.orderflow.controller.base.ABaseController;
import com.example.orderflow.entity.po.OrderInfo;
import com.example.orderflow.entity.vo.ResponseVO;
import com.example.orderflow.exception.BusinessException;
import com.example.orderflow.service.OrderEquipmentService;
import com.example.orderflow.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName OrderGeneratedController
 * @Description: 设备操作
 * @Since JDK 1.8
 * @Create 2024/7/12 16:07
 */

@RestController
public class DeviceOperateController extends ABaseController {

    @Autowired
    OrderEquipmentService orderEquipmentService;
    @Autowired
    OrderInfoService orderInfoService;

    /**
     * @create: 2024/7/12 16:07
     * @description: 订单设备添加
     * @param map
     * @param: orderId 订单号
     * @param: equipmentTypeSpectrum 设备类型
     * @param: otherParameters 其他参数
     * @param: quantity 数量
     * @param: notes 备注
     * @return orderId
     */
    @PostMapping("/deviceAdd")
    public ResponseVO deviceAdd(@RequestBody Map<String,Object> map) {

        try {

            //判断订单是否存在
            OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderId((String) map.get("orderId"));
            if (orderInfo == null){ throw new BusinessException("订单不存在") ;}

            orderEquipmentService.addOrderEquipment(
                    (String)map.get("orderId"),
                    (String) map.get("equipmentTypeSpectrum"),
                    map.get("otherParameters"),
                    (String)map.get("Quantity"),
                    (String) map.get("Notes")
            );

            return getSuccessResponseVO(map.get("orderId")+"订单"+map.get("Quantity")+"台"+map.get("equipmentTypeSpectrum")+"设备录入成功");

        }catch (Exception e){
            throw new BusinessException("订单不存在或设备录入失败");
        }
    }

}
